const express = require('express')
const app = express()
const morgan  = require('morgan')
const cors = require('cors')

const PORT = process.env.PORT || 9000
const host = process.env.NODE_ENV === "production" ? "106.75.118.142" : "localhost";
const router = require('./router')
const errorHandler = require('./middleware/error-handler')

app.use(morgan('dev'))
app.use(express.json())
app.use(cors())

app.use(router)

// 挂载统一处理服务端错误中间件
app.use(errorHandler())

app.listen(PORT, () => {
  console.log(`Server is running at http://${host}:${PORT}`)
})